package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;

import com.lemeng.dao.AdMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Ad;
import com.lemeng.pojo.Admin;
import com.lemeng.service.AdService;
import com.lemeng.service.AdminService;
import com.lemeng.util.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AdServiceImpl implements AdService {

    @Autowired
    AdMapper adMapper;

    @Override
    public List<Ad> findByPosition(String position) {
        System.out.println("从redis获取数据");
        return (List<Ad>) redisTemplate.boundHashOps(CacheKey.AD).get(position);
    }

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void selectAdRedis() {
       List<String> list=adMapper.selectPosition();
        if (!CollectionUtils.isEmpty(list)) {
            for (String s : list) {
                List<Ad> byPosition = findByPositionForRedis(s);
                redisTemplate.boundHashOps(CacheKey.AD).put(s,byPosition);
            }
        }

    }

    public List<Ad> findByPositionForRedis(String position) {
        Example example = new Example(Ad.class);
        Date date = new Date();
        example.createCriteria()
                .andEqualTo("position",position)
                .andEqualTo("status","1")
                .andLessThanOrEqualTo("startTime",date)
                .andGreaterThanOrEqualTo("endTime",date);

        return   adMapper.selectByExample(example);
    }
}
